Skip to content

Fix _fast_cache invalidation bug in set_input and improve cache keys#448

Merged
MaxGhenis merged 1 commit intoPolicyEngine:masterfrom
MaxGhenis:fix-fast-cache-bugs
Mar 14, 2026
Merged

Fix _fast_cache invalidation bug in set_input and improve cache keys#448
MaxGhenis merged 1 commit intoPolicyEngine:masterfrom
MaxGhenis:fix-fast-cache-bugs

Conversation

@MaxGhenis
Copy link
Contributor

Summary

  • Fix critical bug: set_input() now invalidates _fast_cache entries, preventing stale values from being returned by subsequent calculate() calls
  • Performance improvement: Use Period objects directly as _fast_cache keys instead of str(period)Period is a hashable tuple subclass, so this avoids unnecessary string conversions
  • Style: Use public trace property (via getattr) instead of private _trace attribute in the fast cache check
  • Code deduplication: Extract _build_enum_lut() and _unify_structured_dtypes() helpers in vectorial_parameter_node_at_instant.py to eliminate 3x repeated dtype unification and 2x repeated enum LUT construction logic
  • Tests: Add comprehensive test suite for all _fast_cache behaviors (cache hit, set_input invalidation, delete_arrays invalidation, clone isolation, purge_cache invalidation, key type)

Follows up on #436 (perf optimizations).

Test plan

  • New tests/core/test_fast_cache.py with 7 tests — all pass
  • Full test suite: 477 passed, 2 skipped, 1 xfailed
  • TDD: verified set_input invalidation test FAILS before fix, PASSES after
  • TDD: verified Period-as-key test FAILS before fix, PASSES after

set_input() was not clearing _fast_cache entries, causing stale values
to be returned on subsequent calculate() calls. Also switches cache
keys from str(period) to Period objects (already hashable tuples) to
avoid unnecessary string conversions, deduplicates enum LUT and
structured dtype unification logic in vectorial parameter lookups,
and uses the public trace property instead of the private _trace attr.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@codecov
Copy link

codecov bot commented Mar 14, 2026

Codecov Report

❌ Patch coverage is 84.04255% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.79%. Comparing base (8483579) to head (bc754a4).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
.../parameters/vectorial_parameter_node_at_instant.py 54.83% 13 Missing and 1 partial ⚠️
policyengine_core/simulations/simulation.py 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #448      +/-   ##
==========================================
+ Coverage   82.51%   82.79%   +0.28%     
==========================================
  Files         202      203       +1     
  Lines       10634    10819     +185     
  Branches     1069     1096      +27     
==========================================
+ Hits         8775     8958     +183     
+ Misses       1583     1573      -10     
- Partials      276      288      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MaxGhenis MaxGhenis merged commit ff93ef4 into PolicyEngine:master Mar 14, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant